using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cmc.RabbitMQ.Base;

namespace Cmc.RabbitMQ
{
    public class DefaultConnectionFactory : IConnectionFactory
    {
        static List<IMQConnection> list = new List<IMQConnection>();

        public IMQConnection GetConnection(string hostName, List<ConnectConfig> configs)
        {
            var con = list.FirstOrDefault(m => m.HostName == hostName);
            if (con == null)
            {
                var config = configs.FirstOrDefault(m => m.HostName == hostName);
                if (config == null)
                {
                    throw new Exception($"没有找到{hostName}的主机MQ配置！");
                }

                IMQConnection connection = new Connection(config);
                connection.Connected();
                list.Add(connection);
                return connection;
            }

            return con;
        }
    }
}
